﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace BanSupport
{
    public class BaseObject
    {
        public int id;//唯一且不变
        public string name;
        public string assetName;
        public FixVector2 position = new FixVector2(Fix64.Zero, Fix64.Zero);
        public Fix64 rotation = Fix64.Zero;
        public bool destroy = false;

        public List<BaseAction> actions = new List<BaseAction>();

        public BaseObject(string name, string assetName)
        {
            this.name = name;
            this.assetName = assetName;
            this.id = GameLogic.Instance.globalId;
            GameLogic.Instance.globalId++;
        }

        public virtual void Update()
        {
            if (destroy) { return; }
            if (actions.Count > 0)
            {
                for (int i = actions.Count - 1; i >= 0; i--)
                {
                    if (actions[i].Update())
                    {
                        actions.RemoveAt(i);
                    }
                }
            }
        }

        public T GetActionByType<T>() where T : BaseAction
        {
            foreach (var aAction in actions)
            {
                if (aAction is T)
                {
                    return aAction as T;
                }
            }
            return null;
        }

        public void MoveTo(FixVector2 toPos, Fix64 during,Action completeAction = null)
        {
            var moveAction = GetActionByType<MoveAction>();
            if (moveAction == null)
            {
                actions.Add(new MoveAction(this, toPos, during, completeAction));
            }
            else
            {
                moveAction.Change(toPos, during);
            }
        }

        public void CancelMove()
        {
            var moveAction = GetActionByType<MoveAction>();
            actions.Remove(moveAction);
        }

        public void RotateContinue(Fix64 rotateSpeed, Fix64 during, Action completeAction = null)
        {
            var rotateContinueAction = GetActionByType<RotateContinueAction>();
            if (rotateContinueAction == null)
            {
                actions.Add(new RotateContinueAction(this, rotateSpeed, during, completeAction));
            }
            else
            {
                rotateContinueAction.Change(rotateSpeed, during);
            }
        }

    }

}
